/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.peer.to;

import org.ourgrid.common.interfaces.RemoteWorkerProvider;
import org.ourgrid.common.interfaces.management.RemoteWorkerManagement;
import org.ourgrid.common.interfaces.management.RemoteWorkerManagementClient;
import org.ourgrid.common.interfaces.to.LocalWorkerState;
import org.ourgrid.common.spec.worker.WorkerSpec;
import org.ourgrid.peer.PeerConstants;

import br.edu.ufcg.lsd.commune.identification.DeploymentID;
import br.edu.ufcg.lsd.commune.identification.ServiceID;

/**
 */
public class RemoteAllocableWorker extends AllocableWorker {

	private final RemoteWorkerManagement rwMang;
	private final RemoteWorkerManagementClient rwmc;
	private final RemoteWorkerProvider provider;
	private WorkerSpec spec;
	private final ServiceID providerID;
	private final ServiceID workerServiceID;

	/**
	 * @param workerSpec
	 * @param rwManag
	 * @param rwmc
	 * @param providerID 
	 */
	public RemoteAllocableWorker(WorkerSpec workerSpec, RemoteWorkerManagement rwManag, 
									RemoteWorkerManagementClient rwmc, RemoteWorkerProvider provider, 
									String providerDN, ServiceID providerID, ServiceID workerServiceID) {
		this.spec = workerSpec;
		this.rwMang = rwManag;
		this.rwmc = rwmc;
		this.provider = provider;
		this.providerID = providerID;
		this.workerServiceID = workerServiceID;
		this.setProviderCertificateDN(providerDN);
	}

	/* (non-Javadoc)
	 * @see org.ourgrid.peer.to.AllocableWorker#getWorkerProvider()
	 */
	@Override
	public RemoteWorkerProvider getWorkerProvider() {
		return provider;
	}

	/**
	 * @return
	 */
	public RemoteWorkerManagement getWorkerManagement(){
		return rwMang;
	}

	@Override
	public void workForMyGrid(DeploymentID myGrid) {
		rwMang.workForBroker(rwmc, myGrid.getPublicKey());
	}

	/* (non-Javadoc)
	 * @see org.ourgrid.peer.to.AllocableWorker#getStatus()
	 */
	@Override
	public LocalWorkerState getStatus() {
		return LocalWorkerState.IN_USE;
	}
	
	/* (non-Javadoc)
	 * @see org.ourgrid.peer.to.AllocableWorker#setStatus(org.ourgrid.common.interfaces.to.LocalWorkerState)
	 */
	@Override
	public void setStatus(LocalWorkerState state) {}

	/* (non-Javadoc)
	 * @see org.ourgrid.peer.to.AllocableWorker#isWorkerLocal()
	 */
	@Override
	public boolean isWorkerLocal() {
		return false;
	}

	@Override
	public WorkerSpec getWorkerSpec() {
		return spec;
	}
	
	public void setWorkerSpec(WorkerSpec newSpec) {
		this.spec = newSpec;
	}

	@Override
	public Class<?> getMonitorableType() {
		return RemoteWorkerManagement.class;
	}

	@Override
	public String getMonitorName() {
		return PeerConstants.REMOTE_WORKER_MONITOR_OBJECT_NAME;
	}
	
	public ServiceID getProviderID() {
		return providerID;
	}

	public ServiceID getServiceID() {
		return workerServiceID;
	}
}
